<?php
function get_apps(){
    $folders = [];
    $path = BASE_PATH.'apps';
    if (is_dir($path)) {
        $handler = opendir($path);  
        while (($filename = readdir($handler)) !== false) {
            if ($filename != '.' && $filename != '..' && is_dir($path.'/'.$filename)) {
                $folders[] = ['value'=>$filename,'text'=>$filename];
            }  
        }
    }
   
    success($folders);
}

function get_app_pages($input){
    $folders = [];
    $app = (string)$input['app'];
    $path = BASE_PATH.'apps/'.$app.'/temp';
    if (is_dir($path)) {
        $handler = opendir($path);  
        while (($filename = readdir($handler)) !== false) {
            if ($filename != '.' && $filename != '..') {
                $filename = substr($filename, 0, -4);
                $folders[] = ['value'=>$filename,'text'=>$filename];
            }  
        }
    }
   
    success($folders);
}

function get_app_versions($input){
    $folders = [];
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $path = BASE_PATH.'apps/'.$app.'/data/versions';
    if (is_dir($path)) {
        $handler = opendir($path);
        $i = 0;
        while (($filename = readdir($handler)) !== false) {
            if ($filename != '.' && $filename != '..' && substr($filename, 0, -15) == $page) {
                $filename = substr($filename, -15, -5);
                $folders[] = ['value'=>$filename,'text'=>++$i . ' [' . date('m-d H:i:s',(int)$filename).']'];
            }
        }
    }
    $folders[] = ['value'=>'now','text'=>++$i . '_now'];
   
    success($folders);
}

function delete_app_versions($input){
    $folders = [];
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $version = (string)$input['version'];
    $path = BASE_PATH.'apps/'.$app.'/data/versions';
    if (is_dir($path)) {
        $handler = opendir($path);
        $i = 0;
        while (($filename = readdir($handler)) !== false) {
            if ($filename != '.' && $filename != '..' && substr($filename, 0, -15) == $page) {
                if($version=='' || substr($filename,-15,-5) == $version){
                    unlink($path.'/'.$filename);
                }
            }
        }
    }
    $folders[] = ['value'=>'now','text'=>++$i . '_now'];
   
    success($folders);
}

function get_app_schema($input){
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    
    $path = BASE_PATH.'apps/'.$app.'/schema/'.$page.'.json';
    if(!file_exists($path)){
        fail('file is not exsits!');
    }
    $data = file_get_contents($path);
    
    success(json_decode($data, true));
}

function get_app_data($input){
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $version = (string)$input['version'];
    if($version == 'now'){
        $path = BASE_PATH.'apps/'.$app.'/data/'.$page.'.json';
    }else{
        $path = BASE_PATH.'apps/'.$app.'/data/versions/'.$page.$version.'.json';
    }
    if(!file_exists($path)){
        fail('file is not exsits!');
    }
    $data = file_get_contents($path);
    
    success(json_decode($data, true));
}

function save_app_data($input){
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $data = json_encode($input['data']);
    
    $path = BASE_PATH.'apps/'.$app.'/data/'.$page.'.json';
    $backPath = BASE_PATH.'apps/'.$app.'/data/versions';
    if(file_exists($path)){
        if(!file_exists($backPath)){
            mkdir($backPath);
        }
        rename($path, $backPath.'/'.$page.time().'.json');
    }

    $result = file_put_contents($path, $data);
    
    success($result);
}

function preview_app_www($input){
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $version = (string)$input['version'];
    
    if($version == 'now'){
        $dataPath = BASE_PATH.'apps/'.$app.'/data/'.$page.'.json';
    }else{
        $dataPath = BASE_PATH.'apps/'.$app.'/data/versions/'.$page.$version.'.json';
    }
    
    $tempPath = BASE_PATH.'apps/'.$app.'/temp/'.$page.'.php';
    
    if(!file_exists($dataPath) || !file_exists($dataPath)){
        fail('data or temp file is not exsits!');
    }
    
    $data = json_decode(file_get_contents($dataPath), true);

    display($tempPath, $data);
}

function online_app_www($input){
    $app = (string)$input['app'];
    $page = (string)$input['page'];
    $version = (string)$input['version'];
    
    $html = file_get_contents("http://jsonadmin.hills.org/index.php?c=preview&app=".$app."&page=".$page."&version=".$version);
    
    $path = BASE_PATH.'apps/'.$app.'/www/'.$page.'.html';
    $result = file_put_contents($path, $html);
    
    success($result);
    
}

require BASE_PATH.'admin/third/qiniu/autoload.php';
use Qiniu\Auth;
function get_qiniu_token(){

    $accessKey = 'DgWacxvEV6I5XajvG854ccqz7tA-OsypDsxLRnXW';
    $secretKey = 'VbW5qEbUR3C6FmhGcv3wViWYeu6ubHxu2CB9MvAk';
    $auth = new Auth($accessKey, $secretKey);
    $bucket = 'chinalove';
    // 生成上传Token
    $token = $auth->uploadToken($bucket);
    
    success(["domain"=>"http://og90figjg.bkt.clouddn.com/","token"=>$token]);
}